package com.socialcoco.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.socialcoco.auth.TokenGenerator;
import com.socialcoco.entity.SysToken;
import com.socialcoco.entity.User;
import com.socialcoco.mapper.SysTokenMapper;
import com.socialcoco.service.ShiroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShiroServiceImpl implements ShiroService {
    @Autowired
    private UserServiceImpl userRService;
    @Autowired
    private SysTokenMapper sTokenMapper;
    private  final static int EXPIRE=12;
    @Override
    public User findByUsername(String username) {
        HashMap hashMap = new HashMap();
        hashMap.put("username",username);
        List<User> list = userRService.listByMap(hashMap);
        if(list.size()==0){
            return null;
        }
        return list.get(0);
    }
    @Override
    public User findTel(String tel){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("tel",tel);
        User one = userRService.getOne(queryWrapper);
        if(one!=null){
            return one;
        }
        return null;
    }
    @Override
    public User findWeibo(String weiboId){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("weiboid",weiboId);
        User one = userRService.getOne(queryWrapper);
        if(one!=null){
            return one;
        }
        return null;
    }
    @Override
    public Map<String, Object> createToken(Integer userId) {
        Map<String, Object> result = new HashMap<>();
        //生成一个token
        String token = TokenGenerator.generateValue();
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //过期时间
        LocalDateTime expireTime = now.plusHours(EXPIRE);
        //判断是否生成过token
        HashMap hashMap = new HashMap();
        hashMap.put("user_id",userId);
        SysToken tokenEntity = sTokenMapper.selectById(userId);
        if (tokenEntity == null) {
            tokenEntity = new SysToken();
            tokenEntity.setUserId(userId);
            //保存token
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);
            sTokenMapper.insert(tokenEntity);
        } else {
            //更新token
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);
            sTokenMapper.updateById(tokenEntity);
        }

        result.put("token", token);
        result.put("expire", expireTime);
        return result;
    }

    @Override
    public void logout(String token) {
        SysToken byToken = findByToken(token);
        //生成一个token
        token = TokenGenerator.generateValue();
        //修改token
        byToken.setToken(token);
        //使前端获取到的token失效
        sTokenMapper.insert(byToken);
    }

    @Override
    public SysToken findByToken(String accessToken) {

        try{
            HashMap hashMap = new HashMap();
            hashMap.put("token",accessToken);
            List<SysToken> sTokens = sTokenMapper.selectByMap(hashMap);
            return sTokens.get(0);
        }catch (IndexOutOfBoundsException e){
            return null;
        }

    }

    @Override
    public User findByUserId(Integer userId) {

        return userRService.getById(userId);
    }
}
